package com.cake.liulanxiangzhu.controller;

import com.cake.liulanxiangzhu.pojo.vo.OmsOrderVO;
import com.cake.liulanxiangzhu.service.OmsOrderService;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/exportExcel")
public class ExportExcelController {

    @Autowired
    private OmsOrderService omsOrderService;
    @RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
    public void exportOrderExcel(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        //一、从后台拿数据
        List<OmsOrderVO> omsOrderVOS = omsOrderService.queryAllOrderExport();
        //二、 数据转成excel
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-download");
        String fileName = "订单信息.xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="  + fileName);
        // 第一步：定义一个新的工作簿
        XSSFWorkbook wb = new XSSFWorkbook();
        // 第二步：创建一个Sheet页
        XSSFSheet sheet = wb.createSheet("订单信息");
        sheet.setDefaultRowHeight((short) (2 * 256));//设置行高
        XSSFFont font1 = wb.createFont();
        for (int i = 0; i < omsOrderVOS.size(); i++) {
            sheet.setColumnWidth(i,3000);//设置列宽
        }

        //表头样式
        XSSFFont headFont = wb.createFont();
        headFont.setFontName("微软雅黑");
        headFont.setBold(true);
        headFont.setFontHeightInPoints((short) 11);
        XSSFCellStyle headStyle = wb.createCellStyle();
        headStyle.setFont(headFont);// 设置字体css
        headStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 竖向居中
        headStyle.setAlignment(HorizontalAlignment.CENTER);      // 横向居中
        headStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //数据样式
        XSSFFont font = wb.createFont();
        font.setFontName("微软雅黑");
        font.setFontHeightInPoints((short) 11);
        XSSFCellStyle style = wb.createCellStyle();
        style.setFont(font); // 设置字体css
        style.setVerticalAlignment(VerticalAlignment.CENTER);// 竖向居中
        style.setAlignment(HorizontalAlignment.CENTER); // 横向居中
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);


        String[] heads={"订单ID ","订单编号","用户id","用户姓名","用户手机号码 ","配送方式 ","用户地址","商家地址","支付状态 ","支付状态名称 ",
                "商品原价","运费","优惠金额","实际支付金额","预约时间","下单时间","支付时间","创建时间","修改时间","订单商品ID","商品规格ID"
                ,"商品相册ID","商品规格图片","商品规格题目","产品单价","购买数量"};
        for(int i=0;i<heads.length;i++){
            cell = row.createCell(i);
            cell.setCellValue(heads[i]);
            cell.setCellStyle(headStyle);
        }
        XSSFRow rows;
        XSSFCell cells;
        for (int i = 0; i < omsOrderVOS.size(); i++) {
            // 第三步：在这个sheet页里创建一行
            rows = sheet.createRow(i+1);
            // 第四步：在该行创建一个单元格
            cells = rows.createCell(0);
            // 第五步：在该单元格里设置值
            cells.setCellValue(omsOrderVOS.get(i).getOrderId());
            cells.setCellStyle(style);
            cells = rows.createCell(1);
            cells.setCellValue(omsOrderVOS.get(i).getSn());
            cells.setCellStyle(style);
            cells = rows.createCell(2);
            cells.setCellValue(omsOrderVOS.get(i).getUserId());
            cells.setCellStyle(style);
            cells = rows.createCell(3);
            cells.setCellValue(omsOrderVOS.get(i).getOrderId());
            cells.setCellStyle(style);
            cells = rows.createCell(4);
            cells.setCellValue(omsOrderVOS.get(i).getUserName());
            cells.setCellStyle(style);
            cells = rows.createCell(5);
            cells.setCellValue(omsOrderVOS.get(i).getUserTelephone());
            cells.setCellStyle(style);
            cells = rows.createCell(6);
            cells.setCellValue(omsOrderVOS.get(i).getDelivery());
            cells.setCellStyle(style);
            cells = rows.createCell(7);
            cells.setCellValue(omsOrderVOS.get(i).getUserAddress());
            cells.setCellStyle(style);
            cells = rows.createCell(8);
            cells.setCellValue(omsOrderVOS.get(i).getShopAddress());
            cells.setCellStyle(style);
            cells = rows.createCell(9);
            cells.setCellValue(omsOrderVOS.get(i).getState());
            cells.setCellStyle(style);
            cells = rows.createCell(10);
            cells.setCellValue(omsOrderVOS.get(i).getStateNm());
            cells.setCellStyle(style);
            cells = rows.createCell(11);
            cells.setCellValue(String.valueOf(omsOrderVOS.get(i).getOriginalPrice()));
            cells.setCellStyle(style);
            cells = rows.createCell(12);
            cells.setCellValue(String.valueOf(omsOrderVOS.get(i).getFreight()));
            cells.setCellStyle(style);
            cells = rows.createCell(13);
            cells.setCellValue(String.valueOf(omsOrderVOS.get(i).getDiscount()));
            cells.setCellStyle(style);
            cells = rows.createCell(14);
            cells.setCellValue(String.valueOf(omsOrderVOS.get(i).getActualPay()));
            cells.setCellStyle(style);
            cells = rows.createCell(15);
            cells.setCellValue(omsOrderVOS.get(i).getGmtOrder());
            cells.setCellStyle(style);
            cells = rows.createCell(16);
            cells.setCellValue(omsOrderVOS.get(i).getGmtPay());
            cells.setCellStyle(style);
            cells = rows.createCell(17);
            cells.setCellValue(omsOrderVOS.get(i).getCreatTime());
            cells.setCellStyle(style);
            cells = rows.createCell(18);
            cells.setCellValue(omsOrderVOS.get(i).getUpdateTime());
            cells.setCellStyle(style);
            cells = rows.createCell(19);
            cells.setCellValue(omsOrderVOS.get(i).getOrditemId());
            cells.setCellStyle(style);
            cells = rows.createCell(20);
            cells.setCellValue(omsOrderVOS.get(i).getSkuId());
            cells.setCellStyle(style);
            cells = rows.createCell(21);
            cells.setCellValue(omsOrderVOS.get(i).getAlbumId());
            cells.setCellStyle(style);
            cells = rows.createCell(22);
            cells.setCellValue(omsOrderVOS.get(i).getPicture());
            cells.setCellStyle(style);
            cells = rows.createCell(23);
            cells.setCellValue(omsOrderVOS.get(i).getTitle());
            cells.setCellStyle(style);
            cells = rows.createCell(24);
            cells.setCellValue(omsOrderVOS.get(i).getPrice());
            cells.setCellStyle(style);
            cells = rows.createCell(25);
            cells.setCellValue(omsOrderVOS.get(i).getQuantity());
            cells.setCellStyle(style);

        }
        try {
            OutputStream out = response.getOutputStream();
            wb.write(out);
            out.close();
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
